package com.text.day2_3.book;

import java.util.Arrays;

public class Book implements BookInterface,Comparable<Book>{
    private int id;
    private String name;
    private static Book[] books = new Book[]{new Book()};


    public Book() {
        this.id = 0;
        this.name = "";
    }

    public Book(String name) {
        this.id = 0;
        this.name = name;
    }

    @Override
    public boolean addBook(Book book) {

        for (Book book1 : books) {
            if (book.getName().equals(book1.getName()))
                return false;
        }
        if (!books[0].getName().equals("")){
            books = Arrays.copyOf(books,books.length + 1);

        }
//        books[books.length - 1] = book;   // 这地方不能给址
        books[books.length - 1] = new Book(book.getName());

        for (int i = 0; i < books.length; i++) {
            if (books[i].getId() != i){
                books[books.length - 1].setId(i);
                break;
            }
        }

        Arrays.sort(books);   //需要排一下序
        return true;
    }

    @Override
    public boolean updateBook(Book book) {
        for (Book book1 : books) {
            if (book1.getName().equals(book.getName()))
                return false;
            if (book1.getId() == book.getId()){
                book1.setName(book.getName());
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean deleteBook(String bookId) {
        for (int i = 0; i < books.length; i++) {
            if (bookId.equals(books[i].getId() + "")){
                Book[] books1 = new Book[books.length - 1];
                System.arraycopy(books,0,books1,0,i);
                System.arraycopy(books,i + 1,books1,i,books1.length - i);
                books = books1;
                return true;
            }
//            int[] b = new int[a.length - 1];
//            System.arraycopy(a,si + 1,b,0,b.length - si);
//            System.arraycopy(a,0,b,b.length - si,si);
        }
        return false;
    }

    @Override
    public Book selectOne(String bookId) {
        for (Book book : books) {
            if (bookId.equals(book.getId() + "")){
                return book;
            }
        }
        return null;
    }

    @Override
    public Book[] selectAll() {

        return books;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private static Book[] getBooks() {       //禁止外部修改
        return books;
    }

    private static void setBooks(Book[] books) {
        Book.books = books;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public int compareTo(Book o) {
        int a = this.getId();
        int ao = o.getId();
        return a - ao;
    }
}
